import { observable, action, toJS } from 'mobx';
import * as API from '../apis/subscribe';
import { ComsStore } from './tables/common';
import { Modal } from 'antd';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

export class DocSubscribeStore extends ComsStore {
  @observable title = getLabel(385967, '知识订阅');
  @observable loading = false;
  @observable viewcondition = 3;
  @observable dialogVisible = false;
  @observable subscribeDesc = '';
  @observable selectedRows = [];
  @observable selectedSubject = [];
  @observable tabInfo = [
    {
      viewcondition: 3,
      title: getLabel(385478, '可订阅文档'),
      editable: false,
    },
    {
      viewcondition: 0,
      title: getLabel(17713, '订阅历史'),
      editable: false,
    },
    {
      viewcondition: 1,
      title: getLabel(17714, '订阅批准'),
      editable: false,
    },
    {
      viewcondition: 2,
      title: getLabel(17715, '订阅收回'),
      editable: false,
    },
  ];
  @observable showSearchAd = false;
  @observable showThumbnails = false;
  @observable checkedKeys = [];
  @observable secCategory = '';
  @observable secCategoryName = '';
  @observable selectedTreeKey = [];
  @observable dialogLoading = false;
  @observable recordId = '';
  @observable otherSubscribe = '';
  @observable broswerDatas = [];
  @observable selectedKeys = [];

  @action
  setTitle = (title) => {
    this.title = title;
  }

  setLoading = (loading) => {
    this.loading = loading;
  }

  setState = (params = {}) => {
    Object.keys(params).forEach((key) => {
      this[key] = params[key];
    });
    return this;
  }

  __doSearch = (type, params) => {
  	this.selectedRows = [];
    this.selectedSubject = [];
  	this.doSearch(type, params);
  }

  getTabInfo = () => {
    API.getTabInfo().then((data) => {
      this.tabInfo = data.groupinfo;
      this.loading = false;
    });
    return this;
  }

  // tab切换
  changeTabKey = (viewcondition) => {
    this.broswerDatas = [];
    this.viewcondition = viewcondition;
    this.comsWeaTableStore.setSelectedRowKeys([]);
    this.doSearch('Subscribe', { viewcondition, seccategory: this.secCategory });
    this.getRightMenu('Subscribe', { viewcondition });
    this.getConditionInfo('Subscribe', { viewcondition });
    this.selectedRows = [];
    this.selectedSubject = [];
  }

  // 按钮操作
  optionType = (operationType) => {
    this.dialogLoading = true;
    let selectedIds = toJS(this.comsWeaTableStore.selectedRowKeys);
    let selectedRowKeys = toJS(this.comsWeaTableStore.selectedRowKeys).join(',');
    let otherSub = [];
    if (this.recordId || selectedIds) {
      this.broswerDatas && this.broswerDatas.map((item) => {
        if (item.rowId == this.recordId) {
          otherSub.push(item.otherSubscribe);
        }
        selectedIds && selectedIds.map((selectedId) => {
          if (item.rowId == selectedId) {
            otherSub.push(item.otherSubscribe);
          }
        });
      });
      this.otherSubscribe = otherSub.join('_');
    }
    API.optionType({ ids: this.recordId ? this.recordId : selectedRowKeys, operation: operationType, subscribeDesc: this.subscribeDesc, otherSubscribe: this.otherSubscribe }).then((data) => {
      if (data.api_status === true) {
        this.dialogLoading = false;
        this.setState({ dialogVisible: false });
        this.doSearch('Subscribe', { viewcondition: this.viewcondition });
        this.broswerDatas = [];
      } else {
        Modal.error({
          title: getLabel(15172, '系统提示'),
          content: getLabel(384535, '提交失败！'),
        });
      }
    });
  }

  // 批准
  onAgree = () => {
    this.setState({ subscribeDesc: '' });
    this.optionType('approve');
  }
  // 退回
  onReject = () => {
    this.setState({ subscribeDesc: '' });
    this.optionType('reject');
  }
  // 收回
  onGetBack = () => {
    this.setState({ subscribeDesc: '' });
    this.optionType('getback');
  }
  // 提交
  onSubmit = () => {
    this.setState({ subscribeDesc: '' });
    this.dialogVisible = true;
  }

  // 列表数据处理
  rowSelection = {
    onSelect: (record, selected) => {
     if (selected) {
        let isexists = false;
     	this.selectedRows.map((item) => {
     		if (item.id == record.id) {
     		   isexists = true;
     		}
     	});
     	if (!isexists) {
     		this.selectedRows.push(record);
     		this.selectedSubject.push(record.docsubject);
     	}
     } else {
     	this.selectedRows.map((item, i) => {
     		if (item.id == record.id) {
     			this.selectedRows.splice(i, 1);
     			this.selectedSubject.splice(i, 1);
     		}
     	});
     }


     // this.selectedRows = selectedRows;

      // this.selectedSubject = this.selectedRows && this.selectedRows.map(item => {
     //   return item.docsubject;
     // })
    },
    onSelectAll: (record, selected, selectedRows) => {
    	selectedRows.map((item2) => {
    		let isexists = false;
	    	this.selectedRows.map((item, i) => {
    			if (item.id == item2.id) {
    			   isexists = true;
    				if (!record) {
    					this.selectedRows.splice(i, 1);
    					this.selectedSubject.splice(i, 1);
    				}
    			}
    		});

    		if (record && !isexists) {
    			this.selectedRows.push(item2);
     			this.selectedSubject.push(item2.docsubject);
    		}
    	});

     // if (record) {
     //   this.selectedRows=this.selectedRows.concat(selectedRows);
     //   this.selectedSubject = this.selectedRows && this.selectedRows.map(item => {
     //     return item.docsubject;
     //   })
     // }else{
     //   this.selectedRows=[];
     //   this.selectedSubject=[];
     // }
    },
  }
  getValue = (value) => {
    this.subscribeDesc = value;
  }

  // 弹框提交
  dialogSubmit = () => {
    this.optionType('add');
  }

  // 左侧树点击效果
  treeSelect = (key) => {
    if (key) {
      this.selectedKeys.push(key);
      this.comsWeaTableStore.setSelectedRowKeys([]);
      this.selectedTreeKey = key;
      this.secCategory = this.getKey();
      this.doSearch('Subscribe', { viewcondition: this.viewcondition, seccategory: this.secCategory });
      // 高级搜索显示同步
      this.form.reset();
      this.form.updateFields({
        seccategory: {
          value: this.secCategory,
          valueSpan: this.secCategoryName,
          valueObj: [
            {
              id: this.secCategory,
              name: this.secCategoryName,
            },
          ],
        },
      });
    } else {
      this.selectedKeys = [];
    }
  }

  getKey = () => {
    let keySearch = '';
    this.treeDatas.forEach((item) => {
      if (item.domid === this.selectedTreeKey) {
        keySearch = item.key;
        this.secCategoryName = item.name;
      } else if (item.haschild) {
          const tempkey = this.getItem(item, this.selectedTreeKey);
          tempkey.forEach((i) => {
            if (i.getNode) {
              keySearch = i.item.key;
              this.secCategoryName = i.item.name;
            }
          });
        }
    });
    return keySearch;
  }

  getItem = (item, selectedTreeKey) => {
    let getNode = false;
    let returnItem = [];
    if (item.domid === selectedTreeKey) {
      getNode = true;
      returnItem.push({ item, getNode });
    } else {
      returnItem.push({ item, getNode });
      if (item.haschild) {
        item.childs.forEach((i) => {
          let tempResult = [];
          tempResult = this.getItem(i, selectedTreeKey);
          returnItem = returnItem.concat(tempResult);
        });
      }
    }
    return returnItem;
  }
}
